package com.jstyle.jclifexd.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.daoManager.UserInfoDaoManager;
import com.jstyle.jclifexd.model.UserInfo;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.ImageUtils;
import com.jstyle.jclifexd.utils.ScreenUtils;

public class ShareGpsView extends LinearLayout {
    View constraintLayout;
    Context context;
    String appName;
    String dataTime;
    TextView tvShareUserId;
    TextView tvShareTime;
    private ImageView ivShareUserIcon;

    public ShareGpsView(Context context, String time) {
        super(context);
        appName = context.getString(R.string.app_name);
        dataTime=time;
        init(context);
    }

    private static final String TAG = "ShareBottomView";

    private void init(Context context) {
        constraintLayout = LayoutInflater.from(context).inflate(R.layout.layout_share_top, this, false);
        this.context = context;
        layoutView(constraintLayout);
        tvShareTime=constraintLayout.findViewById(R.id.tv_share_time);
        tvShareUserId =constraintLayout.findViewById(R.id.tv_share_userId);
        ivShareUserIcon =constraintLayout.findViewById(R.id.iv_share_userIcon);

        tvShareTime.setText(DateUtil.getShareShowDate(context,dataTime));
        String uid = NetWorkUtil.getUserId();
        UserInfo userInfo = UserInfoDaoManager.getUserByUid(uid);
        if (userInfo != null) {
            String name = userInfo.getName();
            if (!TextUtils.isEmpty(name)) tvShareUserId.setText(name);
            Uri iconUri = ImageUtils.getIconUri(context, userInfo.getUserId());
            Bitmap bmHead = ImageUtils.findHeadBitmap(context, iconUri);
            bmHead = bmHead == null ? BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.share_icon) : bmHead;
            HeadCircleView circleView = new HeadCircleView(bmHead);
            ivShareUserIcon.setImageDrawable(circleView);

        }
    }

    private void layoutView(View v) {
        int width = ScreenUtils.getScreenWidth(context);
        int height = ScreenUtils.dip2px(context,50);
        v.layout(0, 0, width, height);
        int measuredWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        int measuredHeight = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        v.measure(measuredWidth, measuredHeight);
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }

    public Bitmap getBitmap() {
        int width = constraintLayout.getWidth();
        int height = constraintLayout.getHeight();
        Bitmap originBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(originBitmap);
        constraintLayout.draw(canvas);
        return originBitmap;
    }
}
